home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / postgres / postgre4.z / postgre4 / src / test / testtimer.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-27  |  5.0 KB  |  275 lines

  1. /*====================================================================
  2.  *
  3.  * FILE:
  4.  * testtimer.c
  5.  *
  6.  * IDENTIFICATION:
  7.  * $Header: /private/postgres/src/test/RCS/testtimer.c,v 1.2 1990/08/18 16:10:48 sp Exp $
  8.  *
  9.  * These are some routines that can be used to time postgres.
  10.  *
  11.  * initTimer():
  12.  *    Initialize the timer. The timer starts running...
  13.  *    (can be called more than once...)
  14.  *
  15.  * double getTimer():
  16.  *    get the number of ellapsed seconds since `initTimer()'
  17.  *
  18.  * stopTimer():
  19.  *      stop temporarily the timer
  20.  *
  21.  * restartTimer():
  22.  *    restart the timer after a 'stopTimer()' call.
  23.  * 
  24.  *
  25.  *
  26.  *====================================================================
  27.  */
  28.  
  29. #include <stdio.h>
  30. #include <sys/time.h>
  31. #include <sys/resource.h>
  32.  
  33. /* #define DEBUG */
  34. #define MILLION        1000000L
  35.  
  36. static void zeroTimeVal();
  37. static void copyTimeVal();
  38. static void addTimeVal();
  39. static void subTimeVal();
  40. static void getTimeVal();
  41. static double timeValToDouble();
  42. static void printTimeVal();
  43.  
  44. static int TimerIsRunning = 0;
  45. static struct timeval StartTimeVal;
  46. static struct timeval ElapsedTimeSinceStop;
  47.  
  48. /*----------------------------------------------------
  49.  *
  50.  * zeroTimeVal
  51.  *
  52.  */
  53. static
  54. void
  55. zeroTimeVal(x)
  56. struct timeval *x;
  57. {
  58.     x->tv_sec = 0L;
  59.     x->tv_usec = 0L;
  60. }
  61. /*----------------------------------------------------
  62.  *
  63.  * copyTimeVal
  64.  *
  65.  */
  66. static
  67. void copyTimeVal(src, dest)
  68. struct timeval src;
  69. struct timeval *dest;
  70. {
  71.     dest->tv_sec = src.tv_sec;
  72.     dest->tv_usec = src.tv_usec;
  73. }
  74.  
  75. /*----------------------------------------------------
  76.  *
  77.  * addTimeVal
  78.  *
  79.  * add 2 timeval structs.
  80.  */
  81. static
  82. void
  83. addTimeVal(v1, v2, res)
  84. struct timeval v1;
  85. struct timeval v2;
  86. struct timeval *res;
  87. {
  88.     long usecs;
  89.  
  90. #ifdef DEBUG
  91.     printf("addTimeVal: ");
  92.     printTimeVal(v1); printf("+"); printTimeVal(v2); printf("=");
  93. #endif DEBUG
  94.  
  95.     usecs = v1.tv_usec + v2.tv_usec;
  96.     res->tv_usec = usecs % MILLION;
  97.     res->tv_sec = v1.tv_sec + v2.tv_sec + usecs/MILLION;
  98.  
  99. #ifdef DEBUG
  100.     printTimeVal(*res); printf("\n");
  101. #endif DEBUG
  102. }
  103.  
  104. /*----------------------------------------------------
  105.  *
  106.  * subTimeVal
  107.  *
  108.  * subtract 2 timeval structs.
  109.  */
  110. static
  111. void
  112. subTimeVal(v1, v2, res)
  113. struct timeval v1;
  114. struct timeval v2;
  115. struct timeval *res;
  116. {
  117.     long borrow;
  118.  
  119. #ifdef DEBUG
  120.     printf("subTimeVal: ");
  121.     printTimeVal(v1); printf("-"); printTimeVal(v2); printf("=");
  122. #endif DEBUG
  123.  
  124.     if (v1.tv_usec >= v2.tv_usec)  {
  125.     res->tv_usec = v1.tv_usec - v2.tv_usec;
  126.     borrow = 0L;
  127.     } else {
  128.     res->tv_usec = v1.tv_usec - v2.tv_usec + MILLION;
  129.     borrow = 1L;
  130.     }
  131.     res->tv_sec = v1.tv_sec - v2.tv_sec - borrow;
  132.  
  133. #ifdef DEBUG
  134.     printTimeVal(*res); printf("\n");
  135. #endif DEBUG
  136. }
  137.  
  138. /*------------------------------------------------------
  139.  *
  140.  * getTimeVal
  141.  *
  142.  */
  143. static
  144. void
  145. getTimeVal(res)
  146. struct timeval *res;
  147. {
  148.     struct rusage x, y;
  149.     struct timeval self, children;
  150.  
  151. #ifdef DEBUG
  152.     printf("getTimeVal----in\n");
  153. #endif DEBUG
  154.  
  155.     getrusage(RUSAGE_SELF, &x);
  156.     getrusage(RUSAGE_CHILDREN, &y);
  157.  
  158.     addTimeVal(x.ru_utime, x.ru_stime, &self);
  159.     addTimeVal(y.ru_utime, y.ru_stime, &children);
  160.     addTimeVal(self, children, res);
  161.  
  162. #ifdef DEBUG
  163.     printf("getTimeVal----out, res:");
  164.     printTimeVal(*res); printf("\n");
  165. #endif DEBUG
  166. }
  167.  
  168. /*------------------------------------------------------
  169.  *
  170.  * timeValToDouble
  171.  */
  172. static
  173. double
  174. timeValToDouble(t)
  175. struct timeval t;
  176. {
  177.     double res;
  178.  
  179.     res = (double) t.tv_sec + ((double) t.tv_usec) / ((double)MILLION);
  180. #ifdef DEBUG
  181.     printf("timeValToDouble: struct=");
  182.     printTimeVal(t); printf(", double=%.6f\n", res);
  183. #endif DEBUG
  184.  
  185.     return(res);
  186. }
  187.  
  188. /*------------------------------------------------------
  189.  *
  190.  * printTimeVal
  191.  */
  192. static
  193. void
  194. printTimeVal(t)
  195. struct timeval t;
  196. {
  197.     printf("[%ld,%ld]", t.tv_sec, t.tv_usec);
  198. }
  199.  
  200. /*------------------------------------------------------
  201.  *
  202.  * initTimer
  203.  *
  204.  */
  205. initTimer()
  206. {
  207. #ifdef DEBUG
  208.     printf("initTimer--------in\n");
  209. #endif DEBUG
  210.     getTimeVal(&StartTimeVal);
  211.     zeroTimeVal(&ElapsedTimeSinceStop);
  212.     TimerIsRunning = 1;
  213. #ifdef DEBUG
  214.     printf("initTimer--------out\n");
  215. #endif DEBUG
  216. }
  217.  
  218. /*------------------------------------------------------
  219.  *
  220.  * getTimer
  221.  *
  222.  */
  223. double
  224. getTimer()
  225. {
  226.     struct timeval now, res;
  227.     double d;
  228.  
  229. #ifdef DEBUG
  230.     printf("getTimer--------in\n");
  231. #endif DEBUG
  232.  
  233.     if (TimerIsRunning) {
  234.     getTimeVal(&now);
  235.     subTimeVal(now, StartTimeVal, &res);
  236.     addTimeVal(ElapsedTimeSinceStop, res, &res);
  237.     } else {
  238.     copyTimeVal(ElapsedTimeSinceStop, &res);
  239.     }
  240.  
  241.     d = timeValToDouble(res);
  242.  
  243. #ifdef DEBUG
  244.     printf("getTimer--------out, ret=%.6f\n", d);
  245. #endif DEBUG
  246.  
  247.     return(d);
  248. }
  249.  
  250. /*------------------------------------------------------
  251.  *
  252.  * stopTimer
  253.  *
  254.  */
  255. stopTimer()
  256. {
  257.     struct timeval now, interval;
  258.  
  259.     getTimeVal(&now);
  260.     TimerIsRunning = 0;
  261.     subTimeVal(now, StartTimeVal, &interval);
  262.     addTimeVal(ElapsedTimeSinceStop, interval, &ElapsedTimeSinceStop);
  263. }
  264.  
  265. /*------------------------------------------------------
  266.  *
  267.  * restartTimer
  268.  *
  269.  */
  270. restartTimer()
  271. {
  272.     getTimeVal(&StartTimeVal);
  273.     TimerIsRunning = 1;
  274. }
  275.